package com.kob.backend.service.impl.car;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.GetObjectRequest;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kob.backend.mapper.CarMapper;
import com.kob.backend.pojo.Car;
import com.kob.backend.service.HTTPService;
import com.kob.backend.service.car.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * @program: backend
 * @description:
 * @author: mqf
 * @create: 2023-03-21 19:48
 **/
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

    @Autowired
    HTTPService httpService;


    public String doPost(String host, int port, String path, Map<String, String> params) throws MalformedURLException {
        String protocol = "http";
        URL url = new URL(protocol, host, port, path);
        System.out.println(url);
        //httpService.doGET(url, params);
        return httpService.doPOST(url, params);
    }

    @Override
    public boolean changeEnergy(Integer id, String offset) {
        Car byId = getById(id);
        String old = byId.getEnergy();
        float v = Float.parseFloat(old);
        float v1 = Float.parseFloat(offset);
        if(v + v1 < 0 || v + v1 > 100) {
            return false;
        }
        byId.setEnergy(String.valueOf(v + v1));
        updateById(byId);
        return true;
    }

    @Override
    public String getEnergyById(Integer id) {
        Car byId = getById(id);
        return byId.getEnergy();
    }

    @Override
    public String getMoneyById(Integer id) {
        Car byId = getById(id);
        return byId.getMoney();
    }

    @Override
    public void readEnergy(String id) throws com.aliyuncs.exceptions.ClientException, IOException {
        //从oss下载能量txt到本地
        String endpoint = "https://oss-cn-chengdu.aliyuncs.com";
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        String bucketName = "packetmqf3";
        String objectName = "energy"+id+".txt";
        String pathName = "D:\\workspace\\java\\backend\\energy1.txt";
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
        try {
            ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(pathName));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        //从本地读取txt存入数据库
        String filePath = "D:\\workspace\\java\\backend\\energy1.txt";
        FileInputStream fin = new FileInputStream(filePath);
        InputStreamReader reader = new InputStreamReader(fin);
        BufferedReader buffReader = new BufferedReader(reader);
        String energy = "";
        while((energy = buffReader.readLine())!=null){
            System.out.println(energy);
            Car byId = getById(id);
            byId.setEnergy(energy);
        }
        buffReader.close();

    }


}
